<template>
  <div />
</template>

<script>
import * as echarts from 'echarts'
import resize from '@/components/Mixins/resize'
export default {
  mixins: [resize],
  props: {
    chartData: {
      type: Object,
      required: true
    },
    unit: {
      type: String,
      default: ''
    }
  },
  data() {
    return {
      chart: null
    }
  },
  watch: {
    chartData: {
      deep: true,
      handler() {
        this.initChart()
      }
    }
  },
  mounted() {
    this.$nextTick(() => {
      this.initChart()
    })
  },
  beforeDestroy() {
    if (!this.chart) {
      return
    }
    this.chart.dispose()
    this.chart = null
  },
  methods: {
    initChart() {
      if (!this.chart) {
        this.chart = echarts.init(this.$el)
      }
      if (this.chartData) {
        this.setOptions(this.chartData)
      }
    },
    setOptions({ dataList, dateList } = {}) {
      const that = this
      const option = {
        xAxis: {
          data: dateList,
          boundaryGap: false,
          axisTick: {
            show: false
          },
          axisLine: {
            lineStyle: {
              color: '#909399',
              fontSize: 10
            }
          }
        },
        grid: {
          left: 10,
          right: 10,
          bottom: 20
          // top: 5
          // containLabel: true
        },
        tooltip: {
          trigger: 'axis',
          formatter(params) {
            for (const x in params) {
              return that.$t('date') + ':' + params[x].axisValueLabel + ';' + that.$t('data') + ':' + params[x].value + ' ' + that.unit
            }
          }
        },
        yAxis: {
          show: false
        },
        series: {
          type: 'line',
          itemStyle: {
            normal: {
              color: '#03c2c2',
              areaStyle: {
                color: '#e8f8f1'
              }
            }
          },
          smooth: true,
          data: dataList,
          animationDuration: 2800,
          animationEasing: 'cubicInOut'
          // label: {
          //   show: true,
          //   position: 'bottom', // 位置
          //   color: '#1CD8A8',
          //   fontSize: 10,
          //   distance: 300 // 距离
          // } // 柱子上方的数值
        }
      }

      setTimeout(() => {
        that.chart.setOption(option)
      }, 500)
    }
  }
}
</script>
